package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.vo.CountByDayVo;
import com.atguigu.jxc.entity.vo.CountByMonthVO;
import com.atguigu.jxc.entity.vo.SaleCountVo;
import com.atguigu.jxc.service.SaleListService;
import com.atguigu.jxc.util.BigDecimalUtil;
import com.atguigu.jxc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-29 23:24 星期日
 * description:
 */
@Service
public class SaleListServiceImpl implements SaleListService {

    @Autowired
    SaleListDao saleListDao;

    @Autowired
    SaleListGoodsDao saleListGoodsDao;

    /**
     * 销售单查询（可查询条件：销售单号、客户、付款状态）
     * @param saleNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String saleNumber,
                                    Integer customerId,
                                    Integer state,
                                    String sTime,
                                    String eTime) {
        Map<String,Object> map = new HashMap<>();

        List<SaleList> saleLists = saleListDao.list(saleNumber,customerId,state,sTime,eTime);
        map.put("rows",saleLists);
        return map;
    }

    /**
     * 删除销售单
     * @param saleListId
     * @return
     */
    @Override
    public ServiceVO delete(Integer saleListId) {

        //根据saleListId获取销售单商品信息
        List<SaleListGoods> list = new ArrayList<>();
        list = saleListGoodsDao.ifHasSaleListGoods(saleListId);

        if (list.size() > 0){
            return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }

        saleListDao.delete(saleListId);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


    /**
     * 支付结算（修改销售单付款状态）
     * @param saleListId
     * @return
     */
    @Override
    public ServiceVO updateState(Integer saleListId) {

        saleListDao.updateState(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


    /**
     * 销售统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @Override
    public String count(String sTime,
                        String eTime,
                        Integer goodsTypeId,
                        String codeOrName) {

        List<SaleCountVo> saleCountVoList = saleListDao.count(sTime,eTime,goodsTypeId,codeOrName);

        return JSON.toJSONString(saleCountVoList);
    }

    /**
     * 按日统计接口
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public String getSaleDataByDay(String sTime, String eTime) throws Exception {

        List<CountByDayVo> list = new ArrayList<>();
        List<CountByDayVo> countByDayVos = saleListDao.getSaleDataByDay(sTime,eTime);

        /*for (CountByDayVo countByDayVo : countByDayVos) {
            countByDayVo.setProfit(countByDayVo.getSaleTotal() - countByDayVo.getPurchasingTotal());
        }*/

        //获取指定日期时间段内的所有日期
        List<String> timeSlotByDayList = DateUtil.getTimeSlotByDay(sTime, eTime);
        for (String s : timeSlotByDayList) {
            boolean flag = false;
            for (int i = 0; i < countByDayVos.size(); i++) {
                CountByDayVo countByDayVo = countByDayVos.get(i);
                if (s.equals(countByDayVo.getDate())){
                    countByDayVo.setProfit(BigDecimalUtil.keepTwoDecimalPlaces(countByDayVo.getSaleTotal() - countByDayVo.getPurchasingTotal()));
                    countByDayVo.setSaleTotal(BigDecimalUtil.keepTwoDecimalPlaces(countByDayVo.getSaleTotal()));
                    countByDayVo.setPurchasingTotal(BigDecimalUtil.keepTwoDecimalPlaces(countByDayVo.getPurchasingTotal()));
                    list.add(countByDayVo);
                    flag = true;
                    break;
                }
            }

            if (!flag){
                CountByDayVo countByDayVo = new CountByDayVo();
                countByDayVo.setDate(s);
                countByDayVo.setSaleTotal(0.0D);
                countByDayVo.setPurchasingTotal(0.0D);
                countByDayVo.setProfit(0.0D);
                list.add(countByDayVo);
            }
        }


        return JSON.toJSONString(list);
    }


    /**
     * 按月统计
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public String getSaleDataByMonth(String sTime, String eTime) throws Exception {
        List<CountByMonthVO> countByMonthVOList = new ArrayList<>();
        List<CountByMonthVO> countByMonthVOS = saleListDao.getSaleDataByMonth(sTime,eTime);

        //获取给定时间段内的所有月份
        List<String> timeSlotByMonth = DateUtil.getTimeSlotByMonth(sTime, eTime);
        for (String s : timeSlotByMonth) {
            boolean flag = false;
            for (CountByMonthVO countByDayVo : countByMonthVOS) {
                if (s.equals(countByDayVo.getDate())){
                    countByDayVo.setProfit(BigDecimalUtil.keepTwoDecimalPlaces(countByDayVo.getSaleTotal() - countByDayVo.getPurchasingTotal()));
                    countByDayVo.setSaleTotal(BigDecimalUtil.keepTwoDecimalPlaces(countByDayVo.getSaleTotal()));
                    countByDayVo.setPurchasingTotal(BigDecimalUtil.keepTwoDecimalPlaces(countByDayVo.getPurchasingTotal()));
                    countByMonthVOList.add(countByDayVo);
                    flag = true;
                    break;
                }
            }

            if (!flag){
                CountByMonthVO countByMonthVO = new CountByMonthVO();
                countByMonthVO.setDate(s);
                countByMonthVO.setSaleTotal(0.0D);
                countByMonthVO.setPurchasingTotal(0.0D);
                countByMonthVO.setProfit(0.0D);
                countByMonthVOList.add(countByMonthVO);
            }
        }
        return JSON.toJSONString(countByMonthVOList);
    }

}
